package com.zadtek.shopmall.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsFragment;
import com.zadtek.shopmall.bean.ProductDetailInfoBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Zackv on 2018/4/17.
 */

public class ProductDetailInfoFragment extends AbsFragment {
    private TextView tvProductInfo,tvProductArgs;
    private ViewPager viewPager;
    private RecyclerView recyclerView;
    private MyViewPagerAdapter adapter;
    private List<View> viewList = new LinkedList<>();
    private List<String> detailsImg = new LinkedList<>();
    private DetailAdapter detailsAdapter;
    private ProductDetailInfoBean productDetailInfoBean;
    @Override
    public int setView() {
        return R.layout.fragment_product_detailinfo;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initLogic();
        requestData();
    }

    private void requestData() {
        Map<String, Object> param = NetTool.newParams();
        param.put("productId",1);
        NetTool.getApi().getproductinfo(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<ProductDetailInfoBean>>() {
                    @Override
                    public void onData(BaseResponse<ProductDetailInfoBean> value) {
                        productDetailInfoBean = value.getData();
                        updateView();
                    }
                });
    }

    private void updateView() {
        detailsImg.addAll(productDetailInfoBean.getDetailsImgs());
        detailsAdapter.notifyDataSetChanged();
    }

    private void initLogic() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                select(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        adapter = new MyViewPagerAdapter(viewList);
        viewPager.setAdapter(adapter);
        tvProductArgs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                select(1);
            }
        });
        tvProductInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                select(0);
            }
        });

        detailsAdapter = new DetailAdapter(R.layout.details_item,detailsImg);
        detailsAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                detailsAdapter.loadMoreEnd();
                detailsAdapter.loadMoreEnd(true );
            }
        },recyclerView);
        detailsAdapter.setEmptyView(R.layout.normal_empty_view);
        recyclerView.setAdapter(detailsAdapter);
    }

    private void select(int position) {
        if(position == 0){
            tvProductInfo.setTextColor(mActivity.getResources().getColor(R.color.color_red));
            tvProductArgs.setTextColor(mActivity.getResources().getColor(R.color.color_text_black5nd));
        }else if(position == 1){
            tvProductInfo.setTextColor(mActivity.getResources().getColor(R.color.color_text_black5nd));
            tvProductArgs.setTextColor(mActivity.getResources().getColor(R.color.color_red));
        }
    }

    private void initView() {
        tvProductArgs= (TextView) findViewById(R.id.tv_product_args);
        tvProductInfo= (TextView) findViewById(R.id.tv_product_info);
        viewPager= (ViewPager) findViewById(R.id.viewpager);
        View v = getLayoutInflater().inflate(R.layout.details,null);
        recyclerView = v.findViewById(R.id.recycleview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        View v2 = getLayoutInflater().inflate(R.layout.normal_empty_view,null);
        viewList.add(v);
        viewList.add(v2);
    }

    private class MyViewPagerAdapter extends PagerAdapter{

        private List<View> mListViews;

        public MyViewPagerAdapter(List<View> mListViews) {
            this.mListViews = mListViews;//构造方法，参数是我们的页卡，这样比较方便。
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object)   {
            container.removeView(mListViews.get(position));//删除页卡
        }


        @Override
        public Object instantiateItem(ViewGroup container, int position) {  //这个方法用来实例化页卡
            container.addView(mListViews.get(position), 0);//添加页卡
            return mListViews.get(position);
        }

        @Override
        public int getCount() {
            return  mListViews.size();//返回页卡的数量
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0==arg1;//官方提示这样写
        }
    }

    private class DetailAdapter extends BaseQuickAdapter<String,BaseViewHolder>{

        public DetailAdapter(int layoutResId, @Nullable List<String> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            Glide.with(mActivity).load(item).placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.iv_img));
        }
    }
}
